﻿// <copyright file="BlogEntryDymanicNodeProvider.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Blog.Services.Infrastructure
{
    using MvcSiteMapProvider;
    using System;
    using System.Linq;
    using System.Collections.Generic;

    using Core.Infrastructure;

    public class BlogEntryDymanicNodeProvider : DynamicNodeProviderBase
    {
        private BlogEntryService    m_blogEntryService;
        private readonly AppCache   m_appCache;

        public BlogEntryDymanicNodeProvider()
        {
            this.m_appCache = new AppCache();
        }

        public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
        {
            if (m_appCache.Exists("BlogEntries"))
                return (List<DynamicNode>)m_appCache.Get("BlogEntries");

            m_blogEntryService = IoC.Get<BlogEntryService>();
            var nodes = new List<DynamicNode>();

            // Main sitemap
            int entitiesCount = m_blogEntryService.GetAll().Count();
            int pagesCount = entitiesCount % 2 == 0 ? entitiesCount / 2 : (entitiesCount + 1) / 2;
            for (int i = 0; i <= pagesCount; i++)
            {
                var entryNode = new DynamicNode { Key = "mainEntry_" + Guid.NewGuid() };
                if (i != 0)
                {
                    entryNode.RouteValues.Add("page", i);
                }
                entryNode.Action = "Index";
                entryNode.Controller = "blog";
                entryNode.Title = "All";
                nodes.Add(entryNode);
            }

            // Sitemap for categories and entry
            var categories = m_blogEntryService.GetAllCategories().ToList();
            foreach (var category in categories)
            {
                var categoryEntries = m_blogEntryService.Get(x => x.CategoryId == category.Id);

                for (int i = 0; i <= categoryEntries.Count(); i++)
                {
                    var categoryNode = new DynamicNode { Key = "category_" + Guid.NewGuid(), Title = category.CategoryName };
                    if (i != 0)
                    {
                        categoryNode.RouteValues.Add("page", i);
                    }
                    categoryNode.RouteValues.Add("category", category.Alias);
                    categoryNode.Action = "Index";
                    categoryNode.Controller = "Blog";
                    nodes.Add(categoryNode);
                   
                    if (categoryEntries.Any())
                    {
                        foreach (var entry in categoryEntries)
                        {
                            var guid = Guid.NewGuid();
                            var entryNode = new DynamicNode { Key = "entry_" + guid, ParentKey = categoryNode.Key };
                            entryNode.RouteValues.Add("category", category.Alias);
                            entryNode.RouteValues.Add("id", entry.HeaderUrl);
                            entryNode.Action = "Entry";
                            entryNode.Controller = "blog";
                            entryNode.Title = entry.Header;
                            nodes.Add(entryNode);
                        }
                    }
                }
            }

            m_appCache.Add("BlogEntries", nodes);
            return nodes;
        }
    }

}